<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class FmwPDDController extends Controller
{
    protected $baseUrl = '/api/pinduoduo/';

    public function index(Request $request)
    {
        $method = str_replace($this->baseUrl, '', $request->getRequestUri());
        if (empty($method)) {
            return [];
        }
        $method = str_replace('/', '_', $method);

        if (!method_exists($this, $method)) {
            echo sprintf("[%s] mock function not exists", $method);
            exit;
        }

        return $this->$method($request);
    }

    // 获取电子面单
    public function wayBill_getWaybill(Request $request)
    {
        $modules = [];
        $wpCode = $request->get('wp_code');
        $tradeOrderInfoDtos = $request->get('trade_order_info_dtos');
        foreach ($tradeOrderInfoDtos as $dto) {
            $printData = [
                "encryptedData" => "4c23c09ff38d4dc86c25aa6abefa895c09536e94037ccddee211384b1d2a1380c17bf9a3bbc6562ce2842e7441ce19a480fa3f2f1594cbfa67e07b64fb774c61fdff555623c46b4cf8f08092666f9359cf561cb9c7225f61fd03ee8d46a0d2a3a08406c14b677014aa90a9c4e99ffe7927eaef7710eb735a50b1d66cf5eb3c811c0d87c7fc200140823b62d4be89b002491f1a08a00e27971d95855726c86f92f7364d49490cc03d26c045953b17fd2a5aa82c6aca49a17bdc290c002621bac1f205ddf770ccd79f84783922e09a0fd6b002619f658a67247e46a3d179d4eb230292ac79ddeec97ae689c6d05b09a9c9546f7002b7872ada232e425f4c741887b8e5f73772cf41eef69b5d5dcdf0269878112d47d700c2d0339654763b54542e5284cfc152eed29cfad0488b4d07d426d20b0ceeff2d27181553aacdfebc48a1237268802c192b6169c5d6ee56be0d0204668fb5f80f0446b7a42fd354421100677c55667d4e3ba7aac0a75e640788689447dd2150e1d735fd8c36f4f9eb7c03c15af0832a502494e1d9cf1f88cd3c65dbfb838152b3a45b99d138c9037e7830f1f35eb7bb7caa6a191ecfdef034f843227ef16e78315fa6063c18a292fe3595bdfe7835e441a167cd5f7080492ad2cb714f19f0267b4f272f18ab288ea2b346a2e2fae462a6c775e8d632ec08e5c34f0b4c9ae0f1d481847970d9fd78fe36f7b173a51e16a934284655aece3b38bea966f6b5359eb0141e24dd7f541a2543a42cdfe5e2ffebd925df759bc80c4da05123cc2b75972ebe1554785c2fd3995b7ea8b7ae7a48b5e6afe78f89c600942f85e2c06128beaa6209ba58450c67f6338254b0593fa829245a24d6bd9852cf359f10379775e07def48ac2fc7d2f67a990311abe57e9ed14f61901ad250797342bbada5c3e52a887f7aa51d842043ac56b4bad2b676e552a51863561d1b51f53c3b0bba83ea68818629047653e727d6ccc123a90587b41513bb48aa9df2af463fe0b513308c24dff7499fc48fd27c88499578605a0a8cb130f2dc96b8294c4bd39a7d90dd89399fb219d4a4104edaa2511dd4d352083898c25f8aed7125a26af34c05a216173c1274f1ed09edca8c69e4db220b1235d24a5a467e41cbfdc271ac61d105d27a8f866462d09ce5f60455221eb5f8596a0feee3aace638c8aad7dbe849fcf233dff8cc4f228aeea5344498022c96423c3c023faecc52c6afeda837f57fa7aa09d3c83e5093269aaa4f9ac5415d8f953ad98bc93d121a5a25872166f595921953f78665d9609f6572af2edea607c55d611471d6cd01342b220b03365039fac990f8180f72c9b7fa115ec6d10be9830b63320c8f69760607033723ef015d920a5c35d8c89546f4d3325568669548168b9448cc1f0a75d70e0a2a6217c87e2286e7b4ab0bf86668fd6eed46601ab406fff05e8746e96b7cd48a358a61413f272ce89c918b5782277f0d7d3168c9206d1e5e777e508258cb385632c0800f0e203d76470345446655aad50ecd1e65938533a2387e519b7aa9e99c9e0cc1f7ef53c31d62bc65c839b1950de4687eadae6ce596f892e6c0d1ec7eaf03397c824393bf7554ac1ed6260749495b72c700ba5a4a1780e45d7f1f847b82a59071d530becc9bbd6f79d66d1082551c2ab9c3cb34e8d509d9bc82487febe224f854d941b23e916fec1b1257fcf9ce4c77d3f34874a1290303be5f28cdcd4aadb8fae62ab550ccf4c433f31b66ebda57fb4469271b11ffe7d8e8c68fb42aef78e3470731289535a9d45e800244dfd477fbc0c215703f5ab2fe24de3b72c6b9f557f6fd3e0ded5c80901af5c86662adaf60a8c86d948d2139aa976cb142237f011116d8a0193c4fd51e57fd1dc6dff8e39cda766?v=6&pv=1",
                "signature" => "BxNLAt4Erk33IQvO/eo4KfSV6N8SQT8I7lW5pFYzzGfy9Cb2LN+uwkII8X5GlKvwZAjZ1UheBNhlfNrLym/y7e0fqmnJ7Jwg9fbuzRnusk3+KPRBZpuem/i+gioIoJcSUOjdkFdu8uf5Dp1H63+S8QzECMKeWid28wrlEFDwMrM=",
                "templateUrl" => $dto['template_url'],
                "ver" => "6"
            ];

            $modules[] = [
                "print_data" => json_encode($printData),
                "waybill_code" => sprintf('XCXD_PDD__%s%s', $wpCode, Str::random(8)),
                "object_id" => $dto['object_id'],
            ];
        }

        $response = [
            "pdd_waybill_get_response" => [
                "request_id" => sprintf('%s%s', date('YmdHis', time()), Str::random(8)),
                "modules" => $modules
            ],
        ];

        return $response;
    }

    // 订单发货通知接口
    public function order_orderShip()
    {
        return [
            "logistics_online_send_response" => [
                "is_success" => true
            ]
        ];
    }

    public function cloud_getIsvCode()
    {
        return [
            'code' => '200',
            'data' => [
                'response' => [
                    'pageCode' => Str::random(16)
                ],
            ]
        ];
    }

    public function oauth_token(Request $request)
    {
//        return [
//            "error_response" => [
//                "error_msg" => "access_token已过期",
//                "sub_msg" => "",
//                "sub_code" => null,
//                "error_code" => 10019,
//                "request_id" => "15440104776643887",
//            ],
//        ];
        $accessToken = date('YmdHis') . Str::random(6);

        return [
            "scope" => [],
            "access_token" => sprintf("pdd_token_%s", $accessToken),
            "expires_in" => 86400,
            "refresh_token" => sprintf("pdd_refresh_token_%s", $accessToken),
            "owner_id" => "445566",
            "owner_name" => "拼多多ZH店铺",
        ];
    }

    public function api_router(Request $request)
    {
        $type = $request->get('type');
        if (empty($type)) {
            return [];
        }

        $method = str_replace('.', '_', $type);
        if (!method_exists($this, $method)) {
            echo sprintf("[%s] mock function not exists", $method);
            exit;
        }

        return $this->$method($request);
    }

    public function pdd_mall_info_get(Request $request)
    {
//        return [
//            "error_response" => [
//                "error_msg" => "access_token已过期",
//                "sub_msg" => "",
//                "sub_code" => null,
//                "error_code" => 10019,
//                "request_id" => "15440104776643887",
//            ],
//        ];
        return [
            "mall_info_get_response" => [
                "logo" => "pdd_zh",
                "mall_character" => 1,
                "mall_desc" => "zh",
                "mall_id" => 445566,
                "mall_name" => "拼多多ZH店铺",
                "merchant_type" => 1,
            ],
        ];
    }

    public function mall_getInfo(Request $request)
    {
        return [
            'mall_info_get_response' => []
        ];
    }
}
